package com.molyfun.parents.activity.organization.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.organization.adapter.SigUpDetailParenteInfoAdapter;
import com.molyfun.parents.activity.signup.SignUpEditeParentInfoActivity;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.bean.SignUpDetailResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.uievent.UIEventListener;
import com.molyfun.parents.view.FullyLinearLayoutManager;
import com.molyfun.parents.view.ThreeTabBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/11/10 0010.
 */

public class SignUpParentDetailInfoFragment extends BaseFragment implements View.OnClickListener, UIEventListener {


    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.edite_button)
    TextView mEdite_button;
    private int mCurrentCategory;
    private String mDepartid;
    private String mEnrollstudnetid;
    private boolean isPrepared;
    private String TAG = "SignUpParentDetailInfoFragment";

    private SigUpDetailParenteInfoAdapter sigUpDetailParenteInfoAdapter;
    private List<SignUpDetailResponse.DataBean.ContactlistpageBean> contactlistpage;
    private SignUpDetailResponse.DataBean.StudentenrolpageBean studentenrolpage;

    public static SignUpParentDetailInfoFragment newInstance(int category, String departid, String enrollstudnetid) {
        SignUpParentDetailInfoFragment studentInfoFragment = new SignUpParentDetailInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("category", category);
        bundle.putString("departid", departid);
        bundle.putString("enrollstudnetid", enrollstudnetid);
        studentInfoFragment.setArguments(bundle);
        return studentInfoFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_parent_info, container, false);
        ButterKnife.inject(this, view);
        isPrepared = true;
        mCurrentCategory = getArguments().getInt("category");
        initView();
        return view;
    }

    private void initView() {

        mDepartid = getArguments().getString("departid");
        mEnrollstudnetid = getArguments().getString("enrollstudnetid");
        mEdite_button.setOnClickListener(this);

        FullyLinearLayoutManager mLayoutManager = new FullyLinearLayoutManager(getActivity());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        sigUpDetailParenteInfoAdapter = new SigUpDetailParenteInfoAdapter(getActivity());
        mRecyclerView.setAdapter(sigUpDetailParenteInfoAdapter);
        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_SIGNUP_EDITE_PARENT_INFO, this);
//        loadData(mEnrollstudnetid);
    }


    public void onParentTabClick(String departid, String enrollstudnetid) {
        System.out.println(TAG + "onParentTabClick()");
        loadData(enrollstudnetid);
    }
/*
     @Override
     public void onResume() {
         super.onResume();
         mHandler.postDelayed(new Runnable() {
             @Override
             public void run() {
                 loadData(mEnrollstudnetid);
             }
         }, 500);
//         loadData(mEnrollstudnetid);
     }*/
    private long lastTimeInt = 0l;

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        if (!isPrepared || !isVisible) {
            System.out.println(TAG + "lazyLoad()----->return");
            return;
        }
        System.out.println(TAG + "lazyLoad()");
        if(lastTimeInt == 0l){
          /*  mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadData(mEnrollstudnetid);
                    lastTimeInt = System.currentTimeMillis();
                    System.out.println(TAG + "lazyLoad()----->1");
                }
            }, 500);*/
            loadData(mEnrollstudnetid);
            lastTimeInt = System.currentTimeMillis();
            System.out.println(TAG + "lazyLoad()----->1");
        }else {
            long nowTime = System.currentTimeMillis();
            if (nowTime - lastTimeInt > 500) {
                loadData(mEnrollstudnetid);
                lastTimeInt = nowTime;
                System.out.println(TAG + "lazyLoad()----->2");
            }
        }
      /*  mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadData(mEnrollstudnetid);
            }
        }, 500);*/
    }

    public static boolean isLoadeData = false;
    private void loadData(String enrollstudnetid) {
        showProgressDialog();
        Call<SignUpDetailResponse> signUpDetailResponseCall = MFPApplication.getInstance().getRestClient().signUpDetailInfo(enrollstudnetid);
        signUpDetailResponseCall.enqueue(new Callback<SignUpDetailResponse>() {
            @Override
            public void onResponse(Call<SignUpDetailResponse> call, Response<SignUpDetailResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    studentenrolpage = response.body().getData().getStudentenrolpage();
                    contactlistpage = response.body().getData().getContactlistpage();
                    if (contactlistpage.size() > 0) {
                        mEdite_button.setVisibility(View.VISIBLE);
                        mEmptyLayout.setVisibility(View.GONE);
                        sigUpDetailParenteInfoAdapter.refreshData(contactlistpage);
                    } else {
                        sigUpDetailParenteInfoAdapter.clearAll();
                        mEdite_button.setVisibility(View.GONE);
                        mEmptyLayout.setVisibility(View.VISIBLE);
                    }
                } else {
                    sigUpDetailParenteInfoAdapter.clearAll();
                    mEdite_button.setVisibility(View.GONE);
                    mEmptyLayout.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<SignUpDetailResponse> call, Throwable t) {
                dismissProgressDialog();
                mEdite_button.setVisibility(View.GONE);
                sigUpDetailParenteInfoAdapter.clearAll();
                mEmptyLayout.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edite_button:
                SignUpEditeParentInfoActivity.startActivity(getActivity(), ThreeTabBar.ThreeTabChooseTab.BAR_TWO, studentenrolpage, (ArrayList) contactlistpage);

                break;
        }
    }

    @Override
    public void handleUIEvent(Message msg) {
        if (msg.what == EventDispatcherEnum.UI_EVENT_SIGNUP_EDITE_PARENT_INFO) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    isLoadeData = true;
                    loadData(mEnrollstudnetid);
                }
            }, 500);
        }
    }

    private Handler mHandler = new Handler();
}
